//
//  SAMySelfCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/14.
//

#import "SAMySelfCell.h"
#import "UILabel+NSMutableAttributedString.h"
#import "BubbleTableView.h"

@interface SAMySelfCell ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIButton *infoBtn;
@property (nonatomic, strong) UIView *imageV;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *switchBtn;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, strong) BubbleTableView *payBubbleView;
@property (nonatomic, assign) NSInteger payRow;

@end

@implementation SAMySelfCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)refreshCellWithTitle:(NSString *)title info:(NSString *)info AndType:(NSInteger)type {
    self.titleLab.text = title;
    self.infoBtn.hidden = YES;
    self.infoBtn.userInteractionEnabled = NO;
    self.switchBtn.hidden = YES;
    self.imageV.hidden = NO;
    self.type = type;
    if (type == 0) {
    
    }else if (type == 1) {
        self.infoBtn.hidden = NO;
        self.infoBtn.userInteractionEnabled = YES;
        NSString *str = @"不抹零";
        if ([info isEqualToString:@"1"]) {
            str = @"抹掉角";
        }else if ([info isEqualToString:@"2"]) {
            str = @"抹掉分";
        }
        [self.infoBtn setTitle:str forState:UIControlStateNormal];
        self.infoBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, kScaleNum(35));
    }else if (type == 2) {
        self.switchBtn.hidden = NO;
        self.imageV.hidden = YES;
        [self.titleLab setAttributedStringWithSubString:@"(销售结算价格四舍五入)" color:kLightgGrayColor font:kFont(14)];
        self.switchBtn.selected = [info isEqualToString:@"1"];
    }else if (type == 3) {
        self.switchBtn.hidden = NO;
        self.imageV.hidden = YES;
        [self.titleLab setAttributedStringWithSubString:@"(向供货商退货)" color:kLightgGrayColor font:kFont(14)];
        self.switchBtn.selected = [info isEqualToString:@"1"];
    }else if (type == 4) {
        self.switchBtn.hidden = NO;
        self.imageV.hidden = YES;
        [self.titleLab setAttributedStringWithSubString:@"(无库存销售)" color:kLightgGrayColor font:kFont(14)];
        self.switchBtn.selected = [info isEqualToString:@"1"];
    }else if (type == 5) {
        self.switchBtn.hidden = NO;
        self.imageV.hidden = YES;
        self.switchBtn.selected = [info isEqualToString:@"1"];
    }else if (type == 6) {
        self.infoBtn.hidden = NO;
        [self.titleLab setAttributedStringWithSubString:@"(调配货时默认结算方式)" color:kLightgGrayColor font:kFont(14)];
        [self.infoBtn setTitle:info forState:UIControlStateNormal];
        self.infoBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, kScaleNum(35));
    }else if (type == 7) {
        self.switchBtn.hidden = NO;
        self.imageV.hidden = YES;
        self.switchBtn.selected = [info isEqualToString:@"1"];
    }else if (type == 8) {
        self.infoBtn.hidden = NO;
        self.infoBtn.userInteractionEnabled = YES;
        NSString *str = @"扫码";
        if ([info isEqualToString:@"1"]) {
            str = @"现金";
        }else if ([info isEqualToString:@"2"]) {
            str = @"刷卡";
        }else if ([info isEqualToString:@"3"]) {
            str = @"汇款";
        }else if ([info isEqualToString:@"4"]) {
            str = @"微信";
        }else if ([info isEqualToString:@"5"]) {
            str = @"支付宝";
        }else if ([info isEqualToString:@"6"]) {
            str = @"余额";
        }else if ([info isEqualToString:@"7"]) {
            str = @"扫码";
        }
        [self.infoBtn setTitle:str forState:UIControlStateNormal];
        self.infoBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, kScaleNum(35));
    }
}

-(void)clickedInfoBtn {
    if (self.type == 8) {
        [self showPayList];
    }else {
        !self.eraseBlock ?: self.eraseBlock();
    }
}

-(void)clickedSwitchBtn:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    NSString *tag = @"0";
    if (sender.selected == YES) {
        tag = @"1";
    }
    if (self.type == 3) {
        !self.negativeInventoryBlock ?: self.negativeInventoryBlock(tag);
    }else if (self.type == 4) {
        !self.negativeSalesBlock ?: self.negativeSalesBlock(tag);
    }else if (self.type == 2) {
        !self.roundedBlock ?: self.roundedBlock(tag);
    }else if (self.type == 5) {
        !self.calculateBlock ?: self.calculateBlock(tag);
    }else if (self.type == 7) {
        !self.individualDiscountBlock ?: self.individualDiscountBlock(tag);
    }
}

-(void)showPayList{
    
    BOOL isScan = [[SAValueManger shareInStance].userInfo.auditStatus isEqualToString:@"1"];
    BOOL isPayChannel = ![[SAValueManger shareInStance].userInfo.payChannel isEqualToString:@"2"];
    if (isScan == YES) {
        isScan = isPayChannel;
    }
//    1-现金 2-刷卡 3-汇款 4-微信 5-支付宝 6-余额 7-扫码
    NSMutableArray *marr = [NSMutableArray array];
    if (isScan == YES) {
        [marr addObject:@"扫码"];
    }
    [marr addObject:@"现金"];
    [marr addObject:@"刷卡"];
    [marr addObject:@"微信"];
    [marr addObject:@"支付宝"];
    
    self.payRow = 0;
    for (int i = 0; i < marr.count; i++) {
        if ([self.infoBtn.titleLabel.text isEqualToString:marr[i]]) {
            self.payRow = i;
        }
    }

    NSInteger height = marr.count * 50 + 10;
    if (height > 190) {
        height = 190;
    }
    NSArray *arr = nil;
    UIView *window = [UIApplication sharedApplication].keyWindow;
    if (!self.payBubbleView) {
        self.payBubbleView = [[BubbleTableView alloc] initWithLocation:(BubbleLocationDown) bubbleAlignment:(BubbleAlignmentRight) width:150 height:height cellClickBlock:^(NSInteger row, NSString * _Nonnull rowString, NSArray *fromDataArray) {
            self.payRow = row;
            !self.payBlock ?: self.payBlock(rowString);
        } hideBlock:^{
            self.payBubbleView = nil;
        }];
        [self.payBubbleView showWithTargetView:self.imageV maskSuperView:window];
        [self.payBubbleView updateBubble:marr fromDataArray:arr];// 更新气泡弹框
        self.payBubbleView.bubbleCellHeight = 50;
        self.payBubbleView.selectRow = self.payRow;
        self.payBubbleView.isBubbleScrollEnabled = YES;
        self.payBubbleView.bubbleCellTextAlignment = NSTextAlignmentCenter;
    }
}

#pragma mark - initUI

- (void)initUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.infoBtn];
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.switchBtn];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
    }];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - kScaleNum(30), kScaleNum(14), kScaleNum(8), kScaleNum(15))];
    imageV.image = [UIImage imageNamed:@"Sale_rightArrow"];
    [self.contentView addSubview:imageV];
    self.imageV = imageV;
    
    [self.infoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(kScaleNum(150));
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(kScaleNum(84));
    }];
}

#pragma mark - lazy init
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = kFont(15);
        _titleLab.textColor = kBlackColor;
    }
    return _titleLab;
}

-(UIButton *)infoBtn {
    if (!_infoBtn) {
        _infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_infoBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        _infoBtn.titleLabel.font = kFont(15);
        _infoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_infoBtn addTarget:self action:@selector(clickedInfoBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _infoBtn;
}

-(UIButton *)switchBtn {
    if (!_switchBtn) {
        _switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_switchBtn setImage:[UIImage imageNamed:@"gray_switch"] forState:UIControlStateNormal];
        [_switchBtn setImage:[UIImage imageNamed:@"green_switch"] forState:UIControlStateSelected];
        [_switchBtn addTarget:self action:@selector(clickedSwitchBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kLineColor;
    }
    return _lineView;
}

@end

